package com.wx.mall.common.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * lixuqiang
 * 工具类
 */
public class WebTools {

    /**
     * 返回数据
     * @param message 内容
     * @param code code
     * @return map
     */
    public static Map<String,Object> returnDate(Object message, Integer code){
        Map<String,Object> map = new HashMap<>();
        map.put("msg",message);
        map.put("code",code);
        return map;
    }

    /**
     * 保存session
     * @param name 名称
     * @param value 值
     */
    public static void setSession(String name,Object value) {
        RequestAttributes requestAttributes= RequestContextHolder.currentRequestAttributes();
        if(requestAttributes != null) {
            requestAttributes.setAttribute(name, value, RequestAttributes.SCOPE_SESSION);
        }
    }

    /**
     * 获取session
     * @param name 名称
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSession(String name) {
        RequestAttributes requestAttributes=RequestContextHolder.currentRequestAttributes();
        if(requestAttributes!=null) {
            return (T) requestAttributes.getAttribute(name, RequestAttributes.SCOPE_SESSION);
        }
        return null;
    }

    /**
     * 删除session
     * @param name 名称
     */
    public static void removeSession(String name) {
        RequestAttributes requestAttributes=RequestContextHolder.currentRequestAttributes();
        if(requestAttributes != null) {
            requestAttributes.removeAttribute(name, RequestAttributes.SCOPE_SESSION);
        }
    }

    /**
     * 获取本地地址
     * @param request
     * @return
     */
    public static String getPath(HttpServletRequest request) {
        return request.getScheme()+"://"+
                request.getServerName()+":"+
                request.getServerPort()+
                request.getContextPath()+"/";
    }

    /**
     * 随机生成id
     * @return
     */
    public static String getRandomID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    //保存文件到本地
    public static boolean cacheFile(MultipartFile filecontent, String filePath, String fileName) throws IOException {
        OutputStream os = null;
        InputStream inputStream = null;
        try {
            inputStream = filecontent.getInputStream();
            String path = filePath;
            byte[] bs = new byte[1024];
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } finally {
            os.close();
            inputStream.close();
        }
        return true;
    }
}
